package com.zjhb.controller;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zjhb.common.dao.BaseDao;
import com.zjhb.common.dao.Page;
import com.zjhb.common.enumutil.UserTypes;
import com.zjhb.common.util.BeanCopy;
import com.zjhb.common.util.ResourceUtil;
import com.zjhb.common.util.ResponseUtil;
import com.zjhb.common.util.StringMyUtil;
import com.zjhb.entity.DepartmentEntity;

import param.UserParam;

@RestController
@RequestMapping("/depart")
public class DepartMentController {
	@Autowired
	BaseDao baseDao;

	@PostMapping("/add")
	public JSONObject apply(DepartmentEntity depart) throws Exception {
		JSONObject j = new JSONObject();
		if (!ResourceUtil.getCurrentUser().getUserType().equals(UserTypes.Admin.getKey())) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "您没有该权限", "");
			return j;
		}
		if (StringMyUtil.isEmpty(depart.getCode())) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "编号必填", "");
			return j;
		}
	if (StringMyUtil.isEmpty(depart.getId())) {
			Criteria crt = baseDao.getCriteria(DepartmentEntity.class);
			crt.add(Restrictions.eq("code", depart.getCode()));
			if ((Long) crt.setProjection(Projections.rowCount()).uniqueResult() > 0) {
				j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "编号重复", "");
				return j;
			}
			baseDao.save(depart);

		} else {
			DepartmentEntity bean = baseDao.getById(DepartmentEntity.class, depart.getId());
			BeanCopy.copyBean2Bean(bean, depart);
			baseDao.update(bean);
		}
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", "");
		return j;
	}

	@PostMapping("/list")
	public JSONObject list(UserParam param) {
		JSONObject j = new JSONObject();
		if (!ResourceUtil.getCurrentUser().getUserType().equals(UserTypes.Admin.getKey())) {
			j = ResponseUtil.getMap(ResponseUtil.OTHER_CODE, "您没有该权限", "");
			return j;
		}
		Page<DepartmentEntity> page = new Page<>();
		Criteria crt = baseDao.getCriteria(DepartmentEntity.class);
		if (StringMyUtil.isNotEmpty(param.getName())) {
			crt.add(Restrictions.like("name", param.getName(), MatchMode.ANYWHERE));
		}
		crt.setMaxResults(param.getLimit());
		crt.setFirstResult(param.getOffset());
		List<DepartmentEntity> list = crt.list();
		Long totalNum = baseDao.getCount(crt);	
		page.setRows(list);
		page.setTotal(totalNum);
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", page);
		return j;
	}
	
	@PostMapping("/select")
	public JSONObject select() {
		JSONObject j = new JSONObject();
		Criteria crt = baseDao.getCriteria(DepartmentEntity.class);
		List<DepartmentEntity> list = crt.list();
		j = ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", list);
		return j;
	}
}
